package com.lenovo.smarttraffic.zm.wei;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.VideoView;

import com.amap.api.maps.MapView;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;
import com.lenovo.smarttraffic.zm.Wei.Api.Api_GetPeccancyType;
import com.lenovo.smarttraffic.zm.Wei.Bean.Bean_GetCarPeccancy;
import com.lenovo.smarttraffic.zm.Wei.Bean.Bean_GetPeccancyType;
import com.lenovo.smarttraffic.zm.Wei.Bean_json.Bean_json_type;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @author zm
 * @version 2.0
 * @date 2020/9/22 11:09
 */
public class ContentActivity extends BaseActivity {
    private TextView tv_time;
    private TextView tv_address;
    private TextView tv_reason, tv_sum;
    private ImageView iv_dianJi;
    private VideoView vv;
    private ImageView iv_next;
    private static int xuhao = 1;
    private String TAG = "ContentActivity";
    private List<Bean_GetCarPeccancy.ROWSDETAILBean> beanList;
    public static List<Integer> Score = new ArrayList<>();
    public int sumScore = 0, sumMone = 0;
    public static List<Integer> Money = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        Serializable carBean = intent.getSerializableExtra("CarBean");
        Bean_GetCarPeccancy bean = (Bean_GetCarPeccancy) carBean;
        beanList = bean.getROWS_DETAIL();
        initView();
        initData();
    }

    private void initData() {
        tv_address.setText("" + beanList.get(0).getPaddr());
        tv_time.setText("" + beanList.get(0).getPdatetime());
        initHttpReason(beanList.get(0).getPcode());
        initAllMoneyandScore();

    }

    private void initAllMoneyandScore() {
        for (int i = 0; i < beanList.size(); i++) {
            initHttpMoneyandScore(beanList.get(i).getPcode());
        }
        Log.e(TAG, "onResponse: " + Score.size() + Money.size());
    }

    private void initHttpMoneyandScore(String pcode) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://192.168.1.110:8088/transportservice/action/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Api_GetPeccancyType api_getPeccancyType = retrofit.create(Api_GetPeccancyType.class);
        Call<Bean_GetPeccancyType> call = api_getPeccancyType.getCall(new Bean_json_type("user1"));
        call.enqueue(new Callback<Bean_GetPeccancyType>() {
            @Override
            public void onResponse(Call<Bean_GetPeccancyType> call, Response<Bean_GetPeccancyType> response) {
                List<Bean_GetPeccancyType.ROWSDETAILBean> rowsDetail = response.body().getROWS_DETAIL();
                for (int i = 0; i < rowsDetail.size(); i++) {
                    if (pcode.equals(rowsDetail.get(i).getPcode())) {
                        Money.add(rowsDetail.get(i).getPmoney());
                        Score.add(rowsDetail.get(i).getPscore());
                    }
                }
                for (int i = 0; i < Money.size(); i++) {
                    sumScore += Money.get(i);
                    sumMone += Score.get(i);
                }
                if (Money.size() == beanList.size()) {
                    tv_sum.setText("违章：" + beanList.size() + "罚款：" + sumMone + "扣分：" + sumScore);
                }
            }

            @Override
            public void onFailure(Call<Bean_GetPeccancyType> call, Throwable t) {

            }
        });
    }

    private void initHttpReason(String pcode) {
//        GetPeccancyType.do
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://192.168.1.110:8088/transportservice/action/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Api_GetPeccancyType api_getPeccancyType = retrofit.create(Api_GetPeccancyType.class);
        Call<Bean_GetPeccancyType> call = api_getPeccancyType.getCall(new Bean_json_type("user1"));
        call.enqueue(new Callback<Bean_GetPeccancyType>() {
            @Override
            public void onResponse(Call<Bean_GetPeccancyType> call, Response<Bean_GetPeccancyType> response) {
                List<Bean_GetPeccancyType.ROWSDETAILBean> rowsDetail = response.body().getROWS_DETAIL();
                for (int i = 0; i < rowsDetail.size(); i++) {
                    if (pcode.equals(rowsDetail.get(i).getPcode())) {
                        tv_reason.setText("" + rowsDetail.get(i).getPremarks());
                    }
                }
            }

            @Override
            public void onFailure(Call<Bean_GetPeccancyType> call, Throwable t) {

            }
        });
    }

    @Override
    protected int getLayout() {
        return R.layout.content_layout;
    }

    private void initView() {
        initToolBar(findViewById(R.id.toolbar), true, "查询结果");
        tv_time = (TextView) findViewById(R.id.tv_time);
        tv_sum = findViewById(R.id.tv_sum);
        tv_address = (TextView) findViewById(R.id.tv_address);
        tv_reason = (TextView) findViewById(R.id.tv_reason);
        iv_dianJi = (ImageView) findViewById(R.id.iv_dianJi);
        vv = (VideoView) findViewById(R.id.vv);
        iv_next = (ImageView) findViewById(R.id.iv_next);
        iv_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tv_address.setText("" + beanList.get(xuhao).getPaddr());
                tv_time.setText("" + beanList.get(xuhao).getPdatetime());
                initHttpReason(beanList.get(xuhao).getPcode());
                xuhao += 1;
            }
        });
    }
}
